package com.vv.tools.common.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;

import java.io.InputStream;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

/**
 * @author liwen
 */
public class JsonUtil {

    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    static {
        // localDate类型序列号与反序列化
        OBJECT_MAPPER.registerModule(getJavaTimeModule());
        // 反序列化时忽略未知属性，也可以通过在POJO类上声明 @JsonIgnoreProperties(ignoreUnknown = true)
        OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        // 只序列化非空的数据
        OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    }

    private JsonUtil() {
    }

    public static JavaTimeModule getJavaTimeModule() {
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        // 注册 LocalDate 的序列化器
        javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ISO_LOCAL_DATE));
        // 注册 LocalDateTime 的序列化器
        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
        // 注册 LocalDate 的反序列化器
        javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ISO_LOCAL_DATE));
        // 注册 LocalDateTime 的反序列化器
        javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
        return javaTimeModule;
    }

    public static <T> T toObject(String json, Class<T> clazzType) {
        if (StringUtils.isEmpty(json)) {
            return null;
        } else {
            try {
                return OBJECT_MAPPER.readValue(json, clazzType);
            } catch (Exception var3) {
                return null;
            }
        }
    }

    public static <T> T toObject(String json, TypeReference<T> typeReference) {
        if (StringUtils.isEmpty(json)) {
            return null;
        } else {
            try {
                return OBJECT_MAPPER.readValue(json, typeReference);
            } catch (Exception var3) {
                return null;
            }
        }
    }

    public static <T> T toObject(InputStream json, Class<T> clazzType) {
        try {
            return OBJECT_MAPPER.readValue(json, clazzType);
        } catch (Exception var3) {
            return null;
        }
    }

    public static <T> T toObject(InputStream json, TypeReference<T> typeReference) {
        try {
            return OBJECT_MAPPER.readValue(json, typeReference);
        } catch (Exception var3) {
            return null;
        }
    }

    public static <T> List<T> toArray(String json, TypeReference<List<T>> typeReference) {
        if (StringUtils.isEmpty(json)) {
            return Collections.emptyList();
        } else {
            try {
                return OBJECT_MAPPER.readValue(json, typeReference);
            } catch (Exception var4) {
                return Collections.emptyList();
            }
        }
    }

    public static String toJsonString(Object obj) {
        try {
            return OBJECT_MAPPER.writeValueAsString(obj);
        } catch (Exception var3) {
            return null;
        }
    }

    public static JsonNode readTreeForString(String str) {
        try {
            return OBJECT_MAPPER.readTree(str);
        } catch (Exception var3) {
            return null;
        }
    }
}
